const {
	Controller
} = require('uni-cloud-router')
const uniID = require('uni-id')
module.exports = class MemberController extends Controller {
	async getWithdrawList() {
		let {
			pageNum = 1,
				pageSize = 10
		} = this.ctx.data
		try {
			let result = await this.service.trade.getWithdrawList({
				pageNum,
				pageSize,
				isMember: true,
				userId: this.ctx.auth.uid
			})
			return {
				code: 200,
				result
			}
		} catch (e) {
			return {
				code: 500,
				message: e.message
			}
		}
	}
	async getShareMan() {
		const {
			shareManId
		} = this.ctx.data

		if (shareManId && !parseInt(shareManId)) {
			return {
				code: 500,
				msg: '参数错误'
			}
		}

		try {
			let shareMan = await this.service.member.setupShareManId({
				memberId: this.ctx.auth.uid,
				shareManId
			})
			return {
				code: 200,
				result: shareMan
			}
		} catch (e) {
			return {
				code: 500,
				msg: e.message
			}
		}
	}
	async getGiftList() {
		const {
			pageNum,
			pageSize
		} = this.ctx.data

		if ((pageNum && !parseInt(pageNum)) || (pageSize && !parseInt(pageSize))) {
			return {
				code: 500,
				msg: '参数错误'
			}
		}
		try {
			let page = await this.service.member.getGiftList({
				receiverId: this.ctx.auth.uid,
				pageNum,
				pageSize
			})
			return {
				code: 200,
				result: page
			}
		} catch (e) {
			return {
				code: 500,
				msg: e.message
			}
		}
	}

	async receiveTicketGift() {
		const {
			giftId
		} = this.ctx.data
		if (!giftId) {
			return {
				code: 500,
				msg: "参数不对"
			}
		}
		try {
			await this.service.goods.receiveTicketGift({
				giftId
			})
			return {
				code: 200
			}
		} catch (e) {
			return {
				code: 500,
				msg: e.message
			}
		}
	}

	async updateUserInfoByWXData() {
		const {
			code,
			wxUserInfo
		} = this.ctx.data
		if (!code || !wxUserInfo) {
			return {
				code: 500,
				msg: "参数错误"
			}
		}
		return this.service.member.updateUserInfoByWXData({
			code,
			wxUserInfo
		})
	}

	async updateUserInfoByTt() {
		const {
			userInfo
		} = this.ctx.data
		console.log(userInfo)
		if (!userInfo) {
			return {
				code: 500,
				msg: "参数错误"
			}
		}
		return this.service.member.updateUserInfoByTt({
			userInfo
		})
	}

	async exchangeGoods() {
		const {
			codeId,
			userId
		} = this.ctx.data
		if (!codeId) {
			return {
				code: 500,
				msg: '参数错误'
			}
		}
		return this.service.member.exchangeGoods({
			codeId,
			userId
		})
	}

	//管理员兑换兑换码
	async exchangeGoodsAdmin() {
		const {
			codeId,
			userId
		} = this.ctx.data
		if (!codeId || !userId) {
			return {
				code: 500,
				msg: '参数错误'
			}
		}
		return this.service.member.exchangeGoodsAdmin({
			codeId,
			userId
		})
	}

	async registerShareExpert() {
		const {
			codeId,
			codeCity
		} = this.ctx.data
		if (!codeId || !codeCity) {
			return {
				code: 500,
				msg: 'codeId和codeCity不能为空'
			}
		}
		return this.service.member.registerShareExpert({
			codeId,
			codeCity
		})
	}

	async getAccount() {
		const {
			codeId
		} = this.ctx.data
		if (!codeId) {
			return {
				code: 500,
				msg: '参数错误'
			}
		}
		return this.service.member.getAccount({
			codeId
		})
	}

	async getMyPosters() {
		const data = this.ctx.data
		return this.service.member.getMyPosters(data.currentCityCode)
	}
	async getMyShareCode() {
		return this.service.member.getMyShareCode()
	}
	async getMyNeedInvoiceOrders() {
		let {
			pageNum,
			pageSize,
			orderType = 'goods',
			createStartTime,
			createEndTime
		} = this.ctx.data
		try {
			let result = {
				code: 200,
				result: {
					total: 0,
					list: []
				}
			}
			let page = await this.service.order.getOrderNeedInvoice({
				memberId: this.ctx.auth.uid,
				type: orderType,
				pageNum,
				pageSize,
				createStartTime,
				createEndTime
			})
			result.result = page
			return result
		} catch (e) {
			return {
				code: 500,
				msg: e.message
			}
		}
	}
	async getMyOrders() {
		const {
			status,
			type,
			pageNum = 1,
			pageSize = 20
		} = this.ctx.data
		if ((status && isNaN(parseInt(status))) ||
			(type && type != 'personal' && type != 'team' && type != 'all') ||
			isNaN(parseInt(pageNum) ||
				isNaN(parseInt(pageSize)))) {
			return {
				code: 500,
				msg: '参数错误'
			}
		}
		return this.service.member.getMemberOrders({
			memberId: this.ctx.auth.uid,
			status,
			type,
			pageNum,
			pageSize
		})
	}
	async updateMobileWithWXData() {
		const {
			code,
			encryptedData,
			iv
		} = this.ctx.data
		return this.service.member.updateMobileWithWXData({
			uid: this.ctx.auth.uid,
			code,
			encryptedData,
			iv
		})
	}

	async getMemberInfo() {
		let data = this.ctx.data
		return this.service.member.getMemberInfo({
			memberId: this.ctx.auth.uid,
			currentCityCode: data.currentCityCode,
			payType: data.payType,
		})
	}

	async getUserWxGzh() {
		const {
			_id
		} = this.ctx.data
		return this.service.member.getUserWxGzh(_id)
	}
	async refreshVipQrcode() {
		return this.service.member.refreshVipQrcode()
	}

	async getMyTeamList() {
		const {
			level,
			pageNum,
			pageSize,
			temporary,
			userId,
			keyword
		} = this.ctx.data
		if ((level && !parseInt(level))) {
			return {
				code: 500,
				msg: "参数不对"
			}
		}
		return this.service.member.getMemberTeamList({
			memberId: this.ctx.auth.uid,
			level,
			pageNum,
			pageSize,
			temporary,
			userId,
			keyword
		})
	}

	async getMyMotorcade() {
		let data = this.ctx.data
		return this.service.member.getMyMotorcade({
			pageNum: data.pageNum,
			pageSize: data.pageSize,
			userLocation: data.userLocation
		})
	}

	async getMyCollect() {
		let data = this.ctx.data
		return this.service.member.getMyCollect({
			user_id: this.ctx.auth.uid,
			collectType: data.collectType,
			pageNum: data.pageNum,
			pageSize: data.pageSize,
			userLocation: data.userLocation
		})
	}

	// 领取优惠券
	async receiveCoupon() {
		let {
			couponId
		} = this.ctx.data

		if (!couponId) return {
			code: 500,
			msg: '参数错误'
		}

		return this.service.member.receiveCoupon(couponId)
	}

	//修改用户的个人信息
	async updateUserinfo() {
		const {
			userinfo
		} = this.ctx.data
		if (!userinfo) {
			return {
				code: 500,
				msg: "参数错误"
			}
		}
		return this.service.member.updateUserinfo({
			userinfo
		})
	}

	//通过id获取用户的个人信息
	async getUserForId() {
		const {
			userId
		} = this.ctx.data
		if (!userId) {
			return {
				code: 500,
				msg: "参数错误"
			}
		}
		return this.service.member.getUserForId({
			userId
		})
	}
	//查询礼包金额统计
	async getUserPackageSearch() {
		return await this.service.member.getUserPackageSearch()
	}


}